package cn.com.generaldata.os.service.indicator_evaluation.coefficient_of_variation;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 人均GDP变异系数评估指标计算类.
 */
public class PercapitaGDPVariatCalculateService extends IndicatorEvaluationCalculateService {

	private String[] cityAreas = ProvinceIndicatorConstant.CITYS;
	private String personName=ProvinceIndicatorConstant.OCEAN_PERSON_NAME;
	private String GDPArea=ProvinceIndicatorConstant.GDP_INDICATOR_NAME;
	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		IndicatorAccountVO iav=BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		Double ySum=0d;
		Map personGDPMap=new HashMap();
		for(String city:cityAreas)
		{
		iav.setAreaId(city);
		//城市人口数
		Double personCity = getInputOrAccountingIndicatorValue(iav, indicatorAccountVO.getYear(),
				personName);
		Validate.isTrue(personCity != null);
		//城市GDP
		Double gdpCity = getInputOrAccountingIndicatorValue(iav, indicatorAccountVO.getYear(),
				GDPArea);
		Validate.isTrue(gdpCity != null);
		//人均GDP
		Double personGDP=gdpCity/personCity;
		personGDPMap.put(city, personGDP);
		ySum+=personGDP;
		}
		Double yAverage=ySum/3;
		Double sum=0d;
		for(String city:cityAreas)
		{
			Double personGDP = (Double)personGDPMap.get(city);
			sum+=Math.pow((personGDP-yAverage),2);
		}
		return Math.sqrt(sum/3)/yAverage;
	}

}
